<template>
  <div class="school">
    <h1>学校名称：{{name}}</h1>
    <h1>学校地址：{{address}}</h1>
    <h2 v-if="studentName">学生的姓名是：{{studentName}}</h2>
  </div>
</template>

<script>
import pubsub from 'pubsub-js'
export default {
    name: "School",
    data(){
      return{
        name:"幸福中学",
        address:"重庆市渝北区",
        studentName:""
      }
    },
    mounted(){
      //消息的订阅
      this.pubsubId=pubsub.subscribe("getStudentName",(msgName,data)=>{
        console.log("getStudentName消息收到了，消息的名字是："+msgName+"，消息的内容是："+data)
        this.studentName=data
      })
    },
    beforeDestroy(){
      //销毁消息订阅
      pubsub.unsubscribe(this.pubsubId)
    }
}
</script>

<style scoped>
.school{
  background-color: skyblue;
}
</style>
